

//
//  ZCTabBar.m
//  zctx
//
//  Created by apple on 16/2/24.
//  Copyright © 2016年 sanliang. All rights reserved.
//

#import "ZCTabBar.h"
#import "YJTabBarButton.h"
@interface ZCTabBar()
@property (nonatomic, weak) UIButton *plusButton;

@property (nonatomic, strong) NSMutableArray *buttons;

@property (nonatomic, weak) UIButton *selectedButton;

@end

@implementation ZCTabBar

@synthesize backgroundView;

-(id)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        backgroundView = [[UIImageView alloc] initWithFrame:self.bounds];
        [self addSubview:backgroundView];
    }
    return self;
}

- (NSMutableArray *)buttons{
    if (_buttons == nil) {
        _buttons = [NSMutableArray array];
    }
    return _buttons;
}

- (void)setBackgroundImage:(UIImage *)img
{
    [backgroundView setImage:img];
}

- (void)setItems:(NSArray *)items{
    _items = items;
    NSLog(@"itemscount = %ld",items.count);
    for (UITabBarItem * item in _items) {
        
        YJTabBarButton *btn = [YJTabBarButton buttonWithType:UIButtonTypeCustom];
        btn.item = item;
        [btn setTitleColor:[UIColor colorWithHexString:@"#404040"] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorWithHexString:@"#FF6C61"] forState:UIControlStateSelected];
        btn.tag = self.buttons.count;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
        if (btn.tag == 0) {
            [self btnClick:btn];
        }
        [self addSubview:btn];
        [self.buttons addObject:btn];
    }
}

// 点击tabBarButton调用
-(void)btnClick:(UIButton *)button{
    _selectedButton.selected = NO;
    button.selected = YES;
    _selectedButton = button;
    
    // 通知tabBarVc切换控制器，
    if ([_delegate respondsToSelector:@selector(tabBar:didClickButton:)]) {
        [_delegate tabBar:self didClickButton:button.tag];
    }
}

//#pragma mark 中间按钮
//- (UIButton *)plusButton{
//    if (_plusButton == nil) {
//        UIButton *btn = [[UIButton alloc]init];
//        [btn setImage:[UIImage imageNamed:@"tab_scan_n"] forState:UIControlStateNormal];
//        btn.adjustsImageWhenHighlighted = NO;
//        //        [btn setImage:[UIImage imageNamed:@"fabu_pressed"] forState:UIControlStateHighlighted];
//        //        [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_mainbtn"] forState:UIControlStateNormal];
//        //        [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_mainbtn"] forState:UIControlStateHighlighted];
//        
//        [btn sizeToFit];
//        
//        // 监听按钮的点击
//        [btn addTarget:self action:@selector(plusClick) forControlEvents:UIControlEventTouchUpInside];
//        
//        _plusButton = btn;
//        
//        [self addSubview:_plusButton];
//        
//    }
//    return _plusButton;
//}
//
//// 点击加号按钮的时候调用
//- (void)plusClick{
//    // modal出控制器
//    if ([_delegate respondsToSelector:@selector(tabBarDidClickPlusButton:)]) {
//        [_delegate tabBarDidClickPlusButton:self];
//    }
//}

// self.items UITabBarItem模型，有多少个子控制器就有多少个UITabBarItem模型
// 调整子控件的位置
- (void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat w = self.bounds.size.width;
    CGFloat btnX = 0;
    CGFloat btnY = 0;
    CGFloat btnW = w / (self.items.count);
    CGFloat btnH = self.bounds.size.height;
    int i = 0;
    // 设置tabBarButton的frame
    for (UIView *tabBarButton in self.buttons) {
        btnX = i * btnW;
        tabBarButton.frame = CGRectMake(btnX, btnY, btnW, btnH);
        i++;
    }
   
}
@end
